﻿using System.Collections.Generic;
using System.Reflection;
using AutoTaskSchedule.Core.Infrastructure;

namespace AutoTaskSchedule.Core.Reflects
{
    public class ReflectFactory : IReflectFactory
    {
        /// <summary>
        /// 反射程序集，获取继承AutoTaskSchedule类的类
        /// </summary>
        /// <param name="assemblyFullName">程序集路径</param>
        /// <returns></returns>
        public static List<IAutoTaskSchedule> Reflect(string assemblyFullName)
        {
            var results = new List<IAutoTaskSchedule>();
            var assembly = Assembly.LoadFile(assemblyFullName);
            var classes = assembly.GetExportedTypes();
            if (classes.Length == 0) return results;

            foreach (var cls in classes)
            {
                if (!cls.IsClass) continue;
                var @interface = cls.GetInterface("IAutoTaskSchedule", false);
                if (@interface == null) continue;
                
                var result = assembly.CreateInstance(cls.FullName) as Infrastructure.AutoTaskSchedule;
                results.Add(result);
            }
            return results;
        }
    }
}